/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       skill_icon.cpp

	$Header: /game/skill_icon.cpp $

	$NoKeywords: $

 ************************************************************************/

// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "skill_icon.h"

#include <assert.h>
#include "bitmap_layer_window.h"
#include "bitmap_group.h"
#include "creature_ability.h"
#include "creature_ability_properties.h"
#include "skill_properties.h"
#include "simple_dialog.h"

// -------------------------------------------------------------------
// window to display a skill icon
// -------------------------------------------------------------------

t_skill_icon::t_skill_icon( t_screen_rect const& rect,
						    t_window* parent )
            : t_bitmap_layer_window( 0, rect.top_left(), parent )
{
	m_height = rect.height();
	initialize();
}

// -------------------------------------------------------------------
// window to display a skill icon
// -------------------------------------------------------------------

t_skill_icon::t_skill_icon( t_skill const& skill, t_screen_rect const& rect,
						    t_window* parent )
            : t_bitmap_layer_window( 0, rect.top_left(), parent )
{
	m_height = rect.height();
	initialize();
	set_skill( skill );
}

// -------------------------------------------------------------------
// window to display a skill icon
// -------------------------------------------------------------------
void t_skill_icon::initialize()
{
	m_skill.skill = k_skill_none;
	m_skill.level = k_mastery_none;
	m_ability = k_ability_none;
	m_level_image = new t_bitmap_layer_window( 0, t_screen_point(0,0), this );
}

// -------------------------------------------------------------------
// window to display a ability icon
// -------------------------------------------------------------------
void t_skill_icon::set_ability( t_creature_ability ability )
{
	if (m_ability == ability && m_skill.skill == k_skill_none)
		return;

	m_skill.skill = k_skill_none;
	m_skill.level = k_mastery_none;
	m_ability = ability;

	m_level_image->set_visible( false );
	m_level_image->set_bitmap( 0 );
	if (m_ability == k_ability_none)
	{
		set_bitmap( 0 );
		set_help_balloon_text( "" );
		return;
	}

	t_screen_point point = to_parent( get_screen_rect().top_left() );

	m_bitmaps = get_ability_icons( m_height );
	set_bitmap( m_bitmaps->find( get_ability_keyword( ability ) ), point );
	set_help_balloon_text( get_ability_name( ability ));
}


// -------------------------------------------------------------------
// window to display a skill icon
// -------------------------------------------------------------------
void t_skill_icon::set_skill( t_skill const& skill )
{
	if (skill.skill == m_skill.skill && skill.level == m_skill.level
		&& m_ability == k_ability_none )
		return;

	m_skill = skill;
	m_ability = k_ability_none;

	if (skill.skill == k_skill_none || skill.level == k_mastery_none)
	{
		set_bitmap( 0 );
		set_help_balloon_text( "" );
		return;
	}

	t_screen_point     point = to_parent( get_screen_rect().top_left() );
	t_screen_point     zero(0,0);
	t_bitmap_group_ptr bitmaps;

	bitmaps = get_skill_icons( skill.skill, m_height );
	m_level_image->set_bitmap( bitmaps->find( get_mastery_keyword( skill.level ) ), zero );
	m_level_image->set_visible( true );
	m_bitmaps = bitmaps;
	set_bitmap( bitmaps->find( get_keyword( skill.skill ) ), point );
	set_help_balloon_text( get_name( skill ) );
}

// -------------------------------------------------------------------
// window to display a skill icon
// -------------------------------------------------------------------
void t_skill_icon::right_button_up( t_mouse_event const& event )
{
	if ( !m_custom_popup_text.empty() )
		show_popup_text( m_custom_popup_text, this );
	else if ( m_skill.skill != k_skill_none )
		show_popup_text( ::get_help_text( m_skill ), this );
	else if (m_ability != k_ability_none )
		show_popup_text( ::get_ability_help( m_ability ), this );
}
